package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;

import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insert(Role role, Long[] permissionIds) {
        roleMapper.insert(role);
        if (permissionIds!=null)
            for (Long permissionId : permissionIds) {
                roleMapper.insertRelation(role.getId(),permissionId);
            }
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteRelation(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role selectByPrimaryKey(Long id) {

        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }


    @Override
    public void updateByPrimaryKey(Role role, Long[] permissionIds) {

        roleMapper.updateByPrimaryKey(role);
        roleMapper.deleteRelation(role.getId());
        if (permissionIds!=null&& permissionIds.length>0)
            for (Long permissionId : permissionIds) {
                roleMapper.insertRelation(role.getId(),permissionId);
            }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPagSize());

        return new PageInfo<>(roleMapper.queryForList(qo));



    }

    @Override
    public List<String> queryRoleSnsByEmployeeId(Long employeeId) {
        return roleMapper.queryRoleSnsByEmployeeId(employeeId);
    }

}
